<?php
$act = filter($_GET['act']);
$field = filter($_GET['field']);
$q = filter($_GET['q']);

if(empty($act)){	
?>		
					<!-- box / title -->
					<div class="title">
						<h5>Data Customer</h5>								
						<div class="search global-nav">
							<form method="GET" class="form-search js-search-form" action="index.php?pages=customer" id="global-nav-search" style="width:350px">
								<input type="hidden" name="pages" value="customer">
								<select name="field" id="birthday_month" aria-label="Month" class="" >
									<option value="" <?php echo ($field=="")?"selected":"";?>>Cari Berdasarkan:</option>
									<option value="nama" <?php echo ($field=="nama")?"selected":"";?>>Nama</option>
									<option value="alamat" <?php echo ($field=="alamat")?"selected":"";?>>Alamat</option>
									<option value="telp" <?php echo ($field=="telp")?"selected":"";?>>Telp</option>
									<option value="perusahaan" <?php echo ($field=="perusahaan")?"selected":"";?>>Perusahaan</option>			
									<option value="status" <?php echo ($field=="status")?"selected":"";?>>Status</option>
								</select>

				            	<label class="visuallyhidden" for="search-query">Search query</label>
				            	<input class="search-input" type="text" id="search-query" placeholder="Search" name="q" value="<?php echo $q;?>">
				            	<span class="search-icon js-search-action">
				              		<button type="submit" class="icon nav-search" tabindex="-1">
				              			<span class="visuallyhidden">Search</span>
				              		</button>
				            	</span>
				            	<input disabled="disabled" class="search-input search-hinting-input" type="text" id="search-query-hint" autocomplete="off" spellcheck="false">				              					              
							</form>
						</div>
					</div>
					<!-- end box / title -->
					<div class="table">										
						<table>
							<thead>
								<tr>
									<th class="selected"><input type="checkbox" class="checkall" /></th>
									<th class="action">No.</th>
									<th class="left">Nama Customer</th>
									<th class="left">Alamat</th>
									<th>Telp.</th>
									<th class="left">Perusahaan</th>
									<th class="left">Remainder</th>
									<th>Status</th>
									<th class="action" colspan="2"><a href="?pages=customer&act=edit" class="icon-plus-sign" title="Tambah Data Baru"></a></th>									
								</tr>
							</thead>
							<tbody>
							<?php			

							$q2=$q;
							if($field=='status'){
								$q2 = (strtolower($q)=='aktif')?'1':'0';
							}
							
							$addQuery='';


							if(!empty($field))
								$addQuery .= " WHERE $field LIKE '%$q2%' ";

							$batas=2;
							$pg=(int) $_GET['pg'];
							if(empty($pg))
							{
								$posisi=0;
								$pg=1;
							}
							else
							{
								$posisi = ($pg-1) * $batas;
							}
							$sqlSup1 = mysql_query("SELECT * FROM tb_customer $addQuery ORDER BY id_customer DESC LIMIT $posisi, $batas");
							
							$no=$posisi+1;
							while($dataSup = mysql_fetch_array($sqlSup1)){								

								$status = ($dataSup['status']==1)? "Aktif":"Non Aktif";
							?>
								<tr>
									<td class="selected"><input type="checkbox" /></td>
									<td class="center"><?php echo $no;?></td>
									<td><?php echo $dataSup['nama'];?></td>
									<td><?php echo $dataSup['alamat'];?></td>
									<td><?php echo $dataSup['telp'];?></td>
									<td><?php echo $dataSup['perusahaan'];?></td>
									<td><?php echo $dataSup['remainder'];?> Hari</td>
									<td><?php echo $status;?></td>	
									<td class="action"><a href="?pages=customer&act=edit&id=<?php echo $dataSup['id_customer'];?>" class="icon-edit" title="Ubah Data"></a></td>
									<td class="action">
										<a href="#" title="Hapus Data" onClick="if(confirm('Anda akan menghapus data ini?')) window.location='?pages=customer&act=del&id=<?php echo $dataSup['id_customer'];?>';" class="icon-trash"></a>
									</td>								
								</tr>
							<?php	
								$no++;				
							}

							$sqlSup2 = mysql_query("SELECT * FROM tb_customer $addQuery ORDER BY id_customer DESC");
							$jumlahData = mysql_num_rows($sqlSup2);

							$file="?pages=customer&field=$field&q=$q";
							$jmlhalaman=ceil($jumlahData/$batas);
	
							?>															
							</tbody>
						</table>
						<!-- pagination -->
						<div class="pagination pagination-left">
							<div class="results">
								<span><?php echo "Showing Results ".($posisi+1)."-".($no-1)." of $jumlahData";?></span>
							</div>
							<ul class="pager">
								<?php
								//link ke halaman sebelumnya (previous)
								if($pg > 1){
									$previous=$pg-1;
									echo "<li><a href=$file&pg=1>&laquo; First</a></li>
									<li><a href=$file&pg=$previous>&laquo; Prev</a></li> ";
								}else{ 
									echo "<li class='disabled'>&laquo; First</li>
									<li class='disabled'>&laquo; Prev</li> ";
								}
								?>

								<?php
								//Link untuk Angka
								$angka=($pg > 3 ? " <li class='separator'>...</li> " : " ");
								for($i=$pg-2;$i<$pg;$i++)
								{
								  if ($i < 1) 
								      continue;
								  $angka .= "<li><a href='$file&pg=$i'>$i</a></li> ";
								}

								$angka .= " <li class='current'>$pg</li>";
								for($i=$pg+1;$i<($pg+3);$i++)
								{
								  if ($i > $jmlhalaman) 
								      break;
								  $angka .= "<li><a href='$file&pg=$i'>$i</a></li> ";
								}

								$angka .= ($pg+2<$jmlhalaman ? " <li class='separator'>...</li> <li><a href='$file&pg=$jmlhalaman'>$jmlhalaman</a></li>" : " ");

								echo "$angka";
								?>
								

								<?php
								//link kehalaman berikutnya (Next)
								if($pg < $jmlhalaman)
								{
									$next=$pg+1;
									echo "<li><a href='$file&pg=$next'>Next &raquo;</a></li>
									<li><a href='$file&pg=$jmlhalaman'>Last &raquo;</a></li>";
								}
								else
								{ 
									echo "<li class='disabled'>Next &raquo;</li>
									<li class='disabled'>Last &raquo;</li> ";
								}		
								?>													
							</ul>
						</div>

						<!-- end pagination -->
						<!-- table action -->
						
						<!-- end table action -->						
					</div>

<?php
}elseif ($act=='edit') {

	$id_customer = (int) filter($_GET['id']);

	$sqlSup = mysql_query("SELECT * FROM tb_customer WHERE id_customer=$id_customer");
	$dataSup = mysql_fetch_array($sqlSup);

	if(!empty($id_customer)){
		?>
		<!-- box / title -->
		<div class="title">
			<h5>Ubah Data Customer</h5>														
		</div>
		<!-- end box / title -->
		<?php
	}else{
		?>
		<!-- box / title -->
		<div class="title">
			<h5>Tambah Data Customer</h5>														
		</div>
		<!-- end box / title -->
		<?php
	}
?>

	<div class="table">
		<form method="POST" action="?pages=customer&act=update">
		<input type="hidden" name="id_customer" value="<?php echo $dataSup['id_customer'];?>">
		<table style="width:auto">
		   	<tbody>
		   		<tr>
					<td><label>Nama Customer</label></td>
					<td>:</td>
					<td><input type="text" value="<?php echo $dataSup['nama'];?>" size="60" name="nama" id="nama" placeholder="Nama Customer" required></td>
			   	</tr>
			   	<tr>
					<td><label>Alamat</label></td>
					<td>:</td>
					<td><input type="text" value="<?php echo $dataSup['alamat'];?>" size="60" name="alamat" id="alamat" placeholder="Alamat Customer" required></td>
			   	</tr>
			   	<tr>
					<td><label>Telp/HP</label></td>
					<td>:</td>
					<td><input type="text" value="<?php echo $dataSup['telp'];?>" size="30" name="telp" id="telp" placeholder="Telp/HP"></td>
			   	</tr>
			   	<tr>
					<td style="vertical-align: top;"><label>Perusahaan</label></td>
					<td>:</td>
					<td><input type="text" value="<?php echo $dataSup['perusahaan'];?>" size="60" name="perusahaan" id="ket" placeholder="Nama Perusahaan"></td>
			   	</tr>
				<tr>
					<td><label>Remainder</label></td>
					<td>:</td>
					<td><input type="text" value="<?php echo $dataSup['remainder'];?>" size="60" name="remainder" id="remainder" placeholder="Remainder Customer" required></td>
			   	</tr>
			   	<tr>
					<td><label>Status</label></td>
					<td>:</td>
				 	<td>
				 		<select name="status" style="width:202px">
							<option value="1" <?php echo $status = ($dataSup['status']==1)? "selected":"";?>>Aktif</option>
							<option value="0" <?php echo $status = ($dataSup['status']==0)? "selected":"";?>>Non Aktif</option>
						</select>
				 	</td> 
			   	</tr>
			 	<tr>
			 		<td>&nbsp;</td>
			 		<td>&nbsp;</td>
			 		<td>
			 			<input type="hidden" value="" size="15" name="id">
						<input type="submit" value="Simpan" name="submit" class="btn btn-primary">
						<input type="Reset" value="Reset" class="btn">
			 		</td>
		   		</tr>
		 	</tbody>
		</table>
		</form>
	</div>
<?php
}elseif ($act=='update') {
	# code...
	$id_customer = (int) $_POST['id_customer'];
	$nama = $_POST['nama'];
	$alamat = $_POST['alamat'];
	$telp = $_POST['telp'];
	$perusahaan = $_POST['perusahaan'];
	$remainder = $_POST['remainder'];
	$status = $_POST['status'];

	if(!empty($id_customer)){
		$update = mysql_query("UPDATE tb_customer SET nama='$nama', alamat='$alamat', telp='$telp', perusahaan='$perusahaan', remainder='$remainder', status='$status' WHERE id_customer=$id_customer");

		if($update)
			sukses('Data berhasil disimpan', '?pages=customer');
	}else{
		$insert = mysql_query("INSERT INTO tb_customer VALUES(NULL, '$nama', '$alamat', '$telp', '$perusahaan', '$remainder', '$status')");

		if($insert)
			sukses('Data berhasil disimpan', '?pages=customer');
	}
	
}elseif ($act=='del') {
	# code...
	$id_customer = (int) filter($_GET['id']);

	$delete = mysql_query("DELETE FROM tb_customer WHERE id_customer=$id_customer");

	if($delete)
		sukses('Data berhasil dihapus', '?pages=customer');
}
?>					